<?php
/*
 * Copyright 2009 Eat Local Food, LLC
 * Copyright (c) 2007 osCommerce (this file was written after
 * code review of osCommerce).
 *
 * This file is part of gwtCommerce.
 *
 * gwtCommerce is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * gwtCommerce is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with gwtCommerce.  If not, see <http://www.gnu.org/licenses/>.
 */

defined('IN_COMPONENT_CONTROLLER') or die();

class CreateProductReviewComponent
{
	function CreateProductReviewComponent()
	{
	}


	function action(&$params, $action='query')
	{
		if ($action == 'EditReview') return $this->edit($params);
		else return $this->query($params);
	}

	function query(&$params) 
	{
		$account_id = (int)$params['accountId'];
		$review_id = (int)$params['reviewId'];

		$subParams['handle'] = 'CreateProductReviewComponent';
		$subParams['languageId'] = (int)$params['languageId'];
		$subParams['productId'] = (int)$params['productId'];
		$subParams['max_rows'] = 1;

		require_once('includes/components/ProductsSearchSubComponent.php');
		$subComponent = new ProductsSearchSubComponent();

		if ($account_id > 0 && $review_id > 0)
		{
			$subParams['accountId'] = $account_id;
			$subParams['reviewId'] = $review_id;
			$subParams['searchReviews'] = true;
		
			return $subComponent->query($subParams);
		}
		else
		{
			$value = $subComponent->query($subParams);
			$value{'CreateProductReviewComponentMetaData'}{'review_text_min'} = (int)REVIEW_TEXT_MIN_LENGTH;
			$value{'CreateProductReviewComponent'}{0}{'reviews_id'}= 0;
			$value{'CreateProductReviewComponent'}{0}{'account_id'}= (int)$account_id;
			$value{'CreateProductReviewComponent'}{0}{'account_name'}= get_account_name($account_id);
			$value{'CreateProductReviewComponent'}{0}{'rating'}= 5;
			$value{'CreateProductReviewComponent'}{0}{'review_text'}= '';
			$value{'CreateProductReviewComponent'}{0}{'review_date_added'}= null;
			$value{'CreateProductReviewComponent'}{0}{'review_date_modified'}= null;
			
			return $value;
		}
	}

	function edit(&$params) 
	{
		$account_id = (int)$params['accountId'];
		$product_id = (int)$params['productId'];
		$language_id = (int)$params['languageId'];
		if ($language_id == 0) $language_id = 1;
		$review_id = (int)$params['reviewId'];
		$rating = (int)$params['reviewRating'];
		if ($rating == 0) $rating = 1;
		else if ($rating > 5) $rating = 5;
		$text = prepare_input($params['reviewText']);

		$errorCode = 0;
		if (strlen($text) < REVIEW_TEXT_MIN_LENGTH) $errorCode = -10;

		if ($account_id > 0 && $product_id > 0)
		{
			if ($errorCode == 0)
			{
				$sqlQuery = 'SELECT op.products_id from ' . TABLE_ORDERS_PRODUCTS . ' op, ' . TABLE_ORDERS . ' o ' .
				' where o.customers_id = \'' . $account_id . '\' ' . 
				'and op.products_id = \'' . $product_id . '\' and op.orders_id = o.orders_id group by op.products_id';

				$dataReturned = mysql_query($sqlQuery) or die(mysql_error());
				$i = 0;
				while($row = mysql_fetch_array($dataReturned))
				{
					$i++;
				}
				if ($i == 0) $errorCode = -20;//User has never ordered this product
			}

			$name = get_account_name($account_id);

			if ($errorCode < 0)
			{
				$subParams['handle'] = 'CreateProductReviewComponent';
				$subParams['languageId'] = $language_id;
				$subParams['productId'] = $product_id;
				$subParams['max_rows'] = 1;
		
				require_once('includes/components/ProductsSearchSubComponent.php');
				$subComponent = new ProductsSearchSubComponent();
				$value = $subComponent->query($subParams);
				$value{'CreateProductReviewComponentMetaData'}{'review_text_min'} = (int)REVIEW_TEXT_MIN_LENGTH;
				$value{'CreateProductReviewComponent'}{0}{'reviews_id'}= $errorCode;
				$value{'CreateProductReviewComponent'}{0}{'account_id'}= (int)$account_id;
				$value{'CreateProductReviewComponent'}{0}{'account_name'}= $name;
				$value{'CreateProductReviewComponent'}{0}{'rating'}= (int)$rating;
				$value{'CreateProductReviewComponent'}{0}{'review_text'}= $text;
				$value{'CreateProductReviewComponent'}{0}{'review_date_added'}= null;
				$value{'CreateProductReviewComponent'}{0}{'review_date_modified'}= null;
				
				return $value;
			}

			$sqlAction = 'insert';
			$where = '';
			if ($review_id > 0)
			{
				$sqlAction = 'update';
				$where = 'reviews_id='.$review_id.' and customers_id='.$account_id;
			}

			$sql_data_array = array();
			if ($sqlAction == 'insert')
			{
				$sql_data_array['products_id'] = $product_id;
				$sql_data_array['customers_id'] = $account_id;
				$sql_data_array['customers_name'] = $name;
				$sql_data_array['date_added'] = 'now()';
				$sql_data_array['last_modified'] = 'now()';
			}
			if ($rating > 0) $sql_data_array['reviews_rating'] = $rating;
			if ($sqlAction == 'update') $sql_data_array['last_modified'] = 'now()';

			db_perform(TABLE_REVIEWS, $sql_data_array, $sqlAction, $where);

			if ($sqlAction == 'insert')
			{
				$review_id = mysql_insert_id();
			}
			else
			{
				$where = 'reviews_id='.$review_id.' and languages_id='.$language_id;
			}

			$sql_data_array = array();
			if ($sqlAction == 'insert')
			{
				$sql_data_array['reviews_id'] = $review_id;
				$sql_data_array['languages_id'] = $language_id;
			}
			$sql_data_array['reviews_text'] = $text;
			db_perform(TABLE_REVIEWS_DESCRIPTION, $sql_data_array, $sqlAction, $where);

			$params['reviewId'] = $reviewId;
			
			remove_cache($languageId . '_' . $productId . '_true', 'ProductComponent');
			remove_cache($languageId . '_' . $productId . '_false', 'ProductComponent');
		
			clean_cache('RandomReviewComponent');

		}
		return $this->query($params);
	}
}
?>
